<?php
/**
 * Database access for user datas
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo
 */
class User_Model_Database extends Zend_Db_Table_Abstract
{
    /**
     * Get all user
     *
     * @return array user list
     */
    public function getUser()
    {
        try {
            $query = $this->_db->select()
                    ->from('login_users',
                           array('users_id', 'users_name'));
            // Return role ID
            $rows = $this->_db->fetchAll($query);
            
            $userList = array();
            
            foreach ($rows as $user) {

                $userList[$user['users_id']] = $user['users_name'];
            }
            return $userList;

        } catch (Exception $e) {
            error_log($e);
            return FALSE;
        }
    }

    /**
     * Get all roles
     *
     * @return array users
     */
    public function getRoles()
    {
        try {
            $query = $this->_db->select()
                    ->from('access_roles',
                           array('roles_id', 'roles_name'));

            $rows = $this->_db->fetchAll($query);
            
            $roleList = array();
            
            foreach ($rows as $user) {

                $roleList[$user['roles_id']] = $user['roles_name'];
            }
            return $roleList;

        } catch (Exception $e) {

            error_log($e);
            return FALSE;
        }
    }
    /**
     * Save new user
     *
     * @param array $newUser
     * @return boolean
     */
    public function saveNewUser(array $newUser = array())
    {
        try {
            $query = array();
            $query['users_name'] = strtolower($newUser['user']);
            $query['users_password'] = md5($newUser['password']);
            $query['users_role'] = (integer) $newUser['role'];

            $this->_db->insert('login_users', $query);
            return TRUE;

        } catch (Exception $e) {
            error_log($e);
            return FALSE;
        }
    }
    /**
     * Delete user
     *
     * @param integer $selectUserId
     * @return boolean
     */
    public function delete($selectUserId)
    {
        try {

            $this->_db
                 ->delete(
                       'login_users',
                       array(
                           'users_id = ?' => (integer) $selectUserId,));

            return TRUE;

        } catch (Exception $e) {
            error_log($e);
            return FALSE;
        }
    }
}